package com.example.littleSpider.myapplication.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.adapter.BuyingSpreeAdapter;
import com.example.littleSpider.myapplication.adapter.MyItemDecoration;
import com.example.littleSpider.myapplication.base.BaseActivity;
import com.example.littleSpider.myapplication.myInterfaces.MethodInTitle;
import com.example.littleSpider.myapplication.view.TitleView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.littlespider.mylibrary.util.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 限时抢购
 * Created by aduser on 2017/12/12.
 */

public class BuyingSpreeActivity extends BaseActivity implements MethodInTitle{

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_view)
    TwinklingRefreshLayout refreshView;
    @BindViews({R.id.rl_will_over, R.id.rl_in_the_sale, R.id.rl_next_up})
    List<RelativeLayout> tagViews;

    List<String> datas = new ArrayList<>();
    private final int WILL_OVER = 0;
    private final int IN_THE_SALE = 1;
    private final int NEXT_UP = 2;
    private int currentPage = -1;
    private BuyingSpreeAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_buying_spree;
    }

    @Override
    protected void initView() {
        titleView.initMyTitle(getString(R.string.flash_sale), this);
        titleView.hideTitleTextImage();
//        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            datas.add("");
        }
        adapter = new BuyingSpreeAdapter(R.layout.item_buying_spree, datas);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //添加布局装饰器, 用来制作item之间的分割线
        mRecyclerView.addItemDecoration(new MyItemDecoration());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                T.show(BuyingSpreeActivity.this, "position:" + position);
                Intent intent = new Intent(BuyingSpreeActivity.this, GoodsDetailsActivity.class);
                startActivity(intent);
                Toasty.success(BuyingSpreeActivity.this, "position" + position).show();
            }
        });
//        adapter.setOn
        refreshView.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                T.show(BuyingSpreeActivity.this, "下拉刷新");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshView.finishRefreshing();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                T.show(BuyingSpreeActivity.this, "上拉加载");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshView.finishLoadmore();
                    }
                }, 1000);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void setGoBack() {
        finish();
    }

    @Override
    public void onRightOne() {
        Intent intent = new Intent(this, MyEspeciallySaleActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRightTwo() {
        Intent intent = new Intent(BuyingSpreeActivity.this, ShoppingCartActivity.class);
        startActivity(intent);
    }

    @Override
    public void onTitleText() {

    }

    @OnClick({R.id.rl_will_over, R.id.rl_in_the_sale, R.id.rl_next_up})
    public void linesClick(View view){
        datas.clear();
        for (int i = 0; i < tagViews.size(); i++) {
            tagViews.get(i).setBackgroundColor(Color.parseColor("#CC222222"));
        }
        switch (view.getId()){
            case R.id.rl_will_over:
                currentPage = WILL_OVER;
                for (int i = 0; i < 3; i++) {
                    datas.add("");
                }
                break;
            case R.id.rl_in_the_sale:
                currentPage = IN_THE_SALE;
                for (int i = 0; i < 5; i++) {
                    datas.add("");
                }
                break;
            case R.id.rl_next_up:
                currentPage = NEXT_UP;
                for (int i = 0; i < 0; i++) {
                    datas.add("");
                }
                break;
        }
        adapter.notifyDataSetChanged();
        changeOption();
    }

    private void changeOption(){
        tagViews.get(currentPage).setBackgroundColor(Color.parseColor("#0092DF"));
    }
}
